from django.db import models

# Create your models here.

# 职级表
class RankModel(models.Model):
    name = models.CharField('职级名称', max_length=28)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)

    class Meta:
        db_table = 'rank_tb'
        verbose_name = '职级表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

# 部门表
class SectionModel(models.Model):
    name = models.CharField('部门名称', max_length=28)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)

    class Meta:
        db_table = 'section_tb'
        verbose_name = '部门表'
        verbose_name_plural = verbose_name

# 职位表
class StationModel(models.Model):
    name = models.CharField('职位名称', max_length=28)
    ranks = models.ForeignKey(RankModel,on_delete=models.CASCADE, verbose_name='所属职级')
    sections = models.ForeignKey(SectionModel,on_delete=models.CASCADE, verbose_name='所属部门')
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)

    class Meta:
        db_table = 'station_tb'
        verbose_name = '职级表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

# 角色表
class RoleModel(models.Model):
    name = models.CharField('角色名称', max_length=256)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)

    class Meta:
        db_table = 'role_tb'
        verbose_name = '角色表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

# 权限表
class PermissionModel(models.Model):
    name = models.CharField('权限名称', max_length=100)
    path = models.CharField('权限', max_length=256)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)
    roles = models.ForeignKey(RoleModel, on_delete=models.CASCADE, verbose_name='所属角色')


    class Meta:
        db_table = 'permission_tb'
        verbose_name = '权限表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

# 菜单管理
class MenuModel(models.Model):
    name = models.CharField('菜单名称', max_length=28)
    menu_url = models.CharField('菜单地址', max_length=256)
    menu_sort = models.IntegerField(verbose_name='排序', null=True)
    icon = models.CharField('菜单图标', max_length=256)
    menus = models.IntegerField(verbose_name='上级菜单', null=True)

    class Meta:
        db_table = 'menu_tb'
        verbose_name = '菜单表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

# 菜单角色表
class MenuRoleModel(models.Model):
    role = models.ForeignKey(RoleModel, on_delete=models.CASCADE, verbose_name='所属角色')
    menu = models.ForeignKey(MenuModel, on_delete=models.CASCADE, verbose_name='所属菜单')

    class Meta:
        db_table = 'menu_role_tb'
        verbose_name = '菜单角色表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s 拥有 %s 菜单' % (self.role.name, self.menu.name)

# 职位角色中间表
class StationRoleModel(models.Model):
    station = models.ForeignKey(StationModel, on_delete=models.CASCADE, verbose_name='职位')
    role = models.ForeignKey(RoleModel, on_delete=models.CASCADE, verbose_name='所属角色')

    class Meta:
        db_table = 'station_role_tb'
        verbose_name = '职位角色表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s 拥有 %s角色'%(self.station.name,self.role.name)

# 员工表
class UserModel(models.Model):

    STATUS_TYPE = (
        (0, '待入职'),
        (1, '实习生'),
        (2, '正式员工'),
        (3, '待离职'),
        (4, '待转正'),
    )

    SEX = (
        (1, '男'),
        (2, '女')
    )

    name = models.CharField('员工姓名', max_length=20)
    mobile = models.CharField('手机号',max_length=11, unique=True)
    password = models.CharField('密码', max_length=256)
    account = models.CharField('账号', max_length=28, unique=True)
    sex = models.SmallIntegerField('性别', choices=SEX, null=True)
    age = models.IntegerField('年龄', null=True)
    birthday = models.DateField('生日', null=True)
    address = models.CharField('住址', max_length=100, null=True)
    email = models.CharField('邮箱', max_length=50, unique=True, null=True)
    is_state = models.SmallIntegerField('状态', choices=STATUS_TYPE, default=1)
    is_delete = models.BooleanField(verbose_name="是否离职", default=False)
    stations = models.ForeignKey(StationModel, on_delete=models.CASCADE, verbose_name='所属职位')

    class Meta:
        db_table = 'user_tb'
        verbose_name = '员工表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 流程表
class FlowModel(models.Model):
    name = models.CharField('流程名称', max_length=50)
    create_time = models.DateTimeField('创建时间', auto_now_add=True)
    is_delete = models.BooleanField(verbose_name='是否删除', default=False)
    sections = models.ForeignKey(SectionModel, on_delete=models.CASCADE, verbose_name='所属部门')

    class Meta:
        db_table = 'flow_tb'
        verbose_name = '流程表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

# 流程节点表
class FlowNodeModel(models.Model):
    name = models.CharField('节点名称', max_length=20)
    stations = models.ForeignKey(StationModel, on_delete=models.CASCADE, verbose_name='职位审批')
    nodes = models.IntegerField('上级节点', default=None, null=True)
    flows = models.ForeignKey(FlowModel, on_delete=models.CASCADE, verbose_name='所属流程')

    class Meta:
        db_table = 'flow_node_tb'
        verbose_name = '流程节点表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

# 节点内容类型表
class NodeTypeModel(models.Model):
    title = models.CharField('标题', max_length=20)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    is_delete = models.BooleanField(verbose_name='是否删除', default=False)
    nodes = models.ForeignKey(FlowNodeModel, on_delete=models.CASCADE, verbose_name='所属节点')

    class Meta:
        db_table = 'node_type_tb'
        verbose_name = '节点内容类型表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title

# 节点内容表
class FlowContentModel(models.Model):
    name = models.CharField('内容', max_length=40)
    type = models.ForeignKey(NodeTypeModel, on_delete=models.CASCADE, verbose_name='所属类型')

    class Meta:
        db_table = 'flow_content_tb'
        verbose_name = '流程内容表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

# 历史审批表
class HistoryModel(models.Model):

    STATUS = (
        (1, '审批中'),
        (2, '审批完成'),
        (3, '驳回'),
    )

    users = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='发起人')
    flows = models.ForeignKey(FlowModel, on_delete=models.CASCADE, verbose_name='发起流程')
    create_time = models.DateTimeField('发起时间', auto_now_add=True)
    is_status = models.SmallIntegerField('状态', choices=STATUS)

    class Meta:
        db_table = 'history_tb'
        verbose_name = '历史审批表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s 发起 %s 流程 已被 %s'%(self.users.name,self.flows.name,self.is_status)

# 节点审批表
class ApprovalModel(models.Model):

    STATUS = (
        (1, '待审批'),
        (2, '审批中'),
        (3, '已同意'),
        (4, '已驳回'),
    )

    historys = models.ForeignKey(HistoryModel, on_delete=models.CASCADE, verbose_name='所属流程实例')
    nodes = models.ForeignKey(FlowNodeModel, on_delete=models.CASCADE, verbose_name='所属节点')
    stations = models.ForeignKey(StationModel, on_delete=models.CASCADE, verbose_name='审批人职位')
    is_status = models.SmallIntegerField('状态', choices=STATUS)
    cause = models.CharField('原因', max_length=128, null=True, default=None)
    create_time = models.DateTimeField('时间', auto_now_add=True)

    class Meta:
        db_table = 'approval_tb'
        verbose_name = '节点审批表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s %s %s'%(self.stations.name,self.is_status,self.nodes.name)


# 操作日志
class HandleModel(models.Model):
    users = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='操作人')
    users_IP = models.CharField('登录ip地址', max_length=100, null=True)
    login_device = models.CharField('登录设备', max_length=256, null=True)
    handle_url = models.CharField('操作路径', max_length=100)
    handle_action = models.CharField('操作动作', max_length=20)
    handle_step = models.CharField('操作描述', max_length=100)
    handle_time = models.DateTimeField('操作日期', auto_now_add=True)

    class Meta:
        db_table = 'handle_tb'
        verbose_name = '操作日志表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s 在 %s 操作了 %s'%(self.users.name,self.handle_time,self.handle_url)


# # 邮件发送人类型表
# class MailTypeModel(models.Model):
#
#     TYPE_PROPLE = (
#         (1, '同事'),
#         (2, '部门'),
#         (3, '职位'),
#         (4, '全部'),
#     )
#
#     type = models.SmallIntegerField('发送人群', choices=TYPE_PROPLE, default=1)
#
#     class Meta:
#         db_table = 'mail_type_tb'
#         verbose_name = '邮件发送人类型表'
#         verbose_name_plural = verbose_name
#
#     def __str__(self):
#         return self.type

# 邮件表
class MailModel(models.Model):

    addresser = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='发件人', related_name='addresser_name')
    title = models.CharField('标题', max_length=20)
    content = models.CharField('发送内容', max_length=256)
    start_time = models.DateTimeField('发送时间', auto_now_add=True)
    is_delete = models.BooleanField(verbose_name='是否删除', default=False)
    recipient = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='收件人', related_name='recipient_name')

    class Meta:
        db_table = 'mail_tb'
        verbose_name = '邮件表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.addresser.name} 发送邮件给 {self.recipient.name}'

# 邮件附件表
class AccessoryModel(models.Model):
    nearby = models.CharField('文件地址', max_length=256)
    mails = models.ForeignKey(MailModel, on_delete=models.CASCADE, verbose_name='所属邮件')

    class Meta:
        db_table = 'accessory_tb'
        verbose_name = '邮件附件表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.nearby} 是 {self.mails.content} 的附件'




